package components.map.grid
{
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.utils.ByteArray;

	public class MapGridController
	{
		private var _layer:Shape;
		
		private var _mapWidth:int;
		private var _mapHeight:int;
		
		private var _gridWidth:int;
		private var _gridHeight:int;
		private var _gridW2:int;
		private var _gridH2:int;
		
		//地图格子宽高数量
		private var _gridRowCount:int;
		
		private var _topCenterX:int;
		private var _topCenterY:int;
		
		public function MapGridController( layer:Shape )
		{
			_layer = layer;
			init();
		}
		
		private function init():void
		{
			_layer.cacheAsBitmap = true;
		}
		
		public function clear():void
		{
			_layer.graphics.clear();
		}
		
		public function reset( mapWidth:int, mapHeight:int, gridWidth:int, gridHeight:int ):void
		{
			_mapWidth = mapWidth;
			_mapHeight = mapHeight;
			
			_topCenterX = mapWidth/2 - gridWidth/2;
			_topCenterY = -mapWidth/4;
			
			_gridWidth = gridWidth;
			_gridHeight = gridHeight;
			_gridW2 = _gridWidth/2;
			_gridH2 = _gridHeight/2;
			
			_gridRowCount = int(mapWidth / _gridWidth) + int(mapHeight / _gridHeight);
			
			drawGrids();
		}
		
		public function get mapWidth():int
		{
			return _mapWidth;
		}
		
		public function get mapHeight():int
		{
			return _mapHeight;
		}
		
		public function get gridRowCount():int
		{
			return _gridRowCount;
		}
		
		public function get gridWidth():int
		{
			return _gridWidth;
		}
		
		public function get gridHeight():int
		{
			return _gridHeight;
		}
		
		private function drawGrids():void
		{
			var g:Graphics = _layer.graphics;
			g.clear();
			g.lineStyle( 1 );
			g.moveTo( _topCenterX, _topCenterY );
			var i:int;
			var arr0:Array;
			var arr1:Array;
			for( i=0; i<=_gridRowCount; i++ )
			{
				arr0 = getXYFromGrid( i, 0 );
				arr1 = getXYFromGrid( i, _gridRowCount );
				g.moveTo( arr0[0] + _gridW2, arr0[1] );
				g.lineTo( arr1[0] + _gridW2, arr1[1] );
			}
			for( i=0; i<=_gridRowCount; i++ )
			{
				arr0 = getXYFromGrid( 0, i );
				arr1 = getXYFromGrid( _gridRowCount, i );
				g.moveTo( arr0[0] + _gridW2, arr0[1] );
				g.lineTo( arr1[0] + _gridW2, arr1[1] );
			}
		}
		
		/**
		 * 获取格子位置 
		 * @param i 格子索引i
		 * @param j 格子索引j
		 * @return 
		 * 
		 */		
		public function getXYFromGrid( i:int, j:int ):Array
		{
			return [_topCenterX + (i-j) * _gridW2, _topCenterY + (i+j) * _gridH2]
		}
		
		public function getCenterXYFromGrid( i:int, j:int ):Array
		{
			var arr:Array = getXYFromGrid( i, j );
			arr[0] += _gridW2;
			arr[1] += _gridH2;
			return arr;
		}
		
		/**
		 * 获取格子索引 
		 * @param x 屏幕x 
		 * @param y 屏幕y
		 * @return 
		 * 
		 */		
		public function getGridFromXY( x:Number, y:Number ):Array
		{
			var i:Number;
			var j:Number;
			x -= _gridW2;
			i = (x-_topCenterX)/_gridW2;
			j = (y-_topCenterY)/_gridH2;
			j = (j-i)/2;
			i = i+j;
			
			return [int(i), int(j)];
		}
		
		/**
		 * 获取屏幕坐标xy所在格子的位置 
		 * @param x 屏幕x 
		 * @param y 屏幕y
		 * @return 
		 * 
		 */		
		public function getGridXYFromXY( x:int, y:int ):Array
		{
			var arr:Array = getGridFromXY( x, y );
			return getXYFromGrid( arr[0], arr[1] );
		}
		
		public function getGridCenterXYFromXY( x:int, y:int ):Array
		{
			var arr:Array = getGridXYFromXY( x, y );
			arr[0] += _gridW2;
			arr[1] += _gridH2;
			return arr;
		}
		
		public function getGridData():ByteArray
		{
			var byte:ByteArray = new ByteArray();
			byte.writeShort( _mapWidth );
			byte.writeShort( _mapHeight );
			byte.writeShort( _gridWidth );
			byte.writeShort( _gridHeight );
			byte.writeShort( _gridRowCount );
			
			return byte;
		}
	}
}
























